<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Monitoring extends MX_Controller {
    
  function __construct() {      
    
    $this->load->library('template');
    $this->load->library('form_eksternal');
    $this->load->library('global_models');
    $this->load->library('PHPExcel');
    $this->load->library('encrypt');
    $this->load->helper('path');
    $this->load->helper('url');
    $this->load->helper('form');
    $this->load->library('parser');
    $this->load->library('session');
    $this->load->library('form_validation');
    $this->load->library('pagination');
    $this->menu = $this->cek();
  }
	public function index($tanggal = false){
//    $this->debug($tanggal, true);
    if($tanggal === FALSE){
      $dated = date('d');
      $tdayd = date('t');
      $monthd = date('m');
      $yeard = date('Y');
      $buland = date('M');
      $comd = date('d M Y');
      $valtampil = date('Y-m-d');
    }
    else{
      $tanggal = str_replace("_", "-", $tanggal);
      $dated = date('d', strtotime($tanggal));
      $tdayd = date('t', strtotime($tanggal));
      $monthd = date('m', strtotime($tanggal));
      $yeard = date('Y', strtotime($tanggal));
      $buland = date('M', strtotime($tanggal));
      $comd = date('d M Y', strtotime($tanggal));
      $valtampil = date('Y-m-d', strtotime($tanggal));
    }
    $list_outlet = "WHERE 1=1 ";
    $hide = "block";
    if($this->global_models->get_field("m_privilege", "name", array("id_privilege" => $this->session->userdata('id_privilege'))) == "Outlet - Owner" OR $this->global_models->get_field("m_privilege", "name", array("id_privilege" => $this->session->userdata('id_privilege'))) == "Outlet - Kepala" OR $this->session->userdata('id') == 28){
      $list_outlet .= " AND A.id_outlet IN (0";
      $no = 0;
      foreach($this->global_models->get_outlet_user($this->session->userdata('id')) as $value){
//        if($no > 0)
          $list_outlet .= ",";
        $list_outlet .= $value->id_outlet;
        $no++;
      }
      $list_outlet .= ")";
      $hide = "none";
//      $this->debug($list_outlet, true);
    }
	
	if($this->session->userdata('id') == 28){
		$hide = "block";
	}
    
    $query = "
      SELECT 
      A.*, 
      MAX(C.tanggal) AS tanggal_max,
      (SUM(C.service) + SUM(C.service_belanja) + SUM(C.belanja)) AS total_transaksi,
      (SUM(C.service) + SUM(C.service_belanja)) AS unit_entry,
      (SUM(C.p_service) + SUM(C.p_service_plus) + SUM(C.p_belanja_plus) + SUM(C.p_belanja)) AS total_omset,
      (SUM(C.service) + SUM(C.service_belanja)/".$dated." * ".$tdayd.") AS ue_omset,
      C.id_actifity_report, A.id_status_outlet
      FROM m_outlet AS A
      LEFT JOIN m_actifity_report AS B ON A.id_outlet = B.id_outlet AND B.month = ".$monthd." AND B.year = ".$yeard."
      LEFT JOIN d_day_actifity_report AS C ON B.id_actifity_report = C.id_actifity_report AND C.tanggal > 0 AND C.tanggal <= ".$dated."
      {$list_outlet}
      AND A.id_status_outlet = 0
      GROUP BY A.id_outlet
      ORDER BY ue_omset DESC
      
      ";
    $data = $this->global_models->get_query($query);
    
//    $query2 = "
//      SELECT 
//      A.*,
//      (SUM(C.p_service) + SUM(C.p_service_plus) + SUM(C.p_belanja_plus) + SUM(C.p_belanja)) AS total_omset
//      FROM m_outlet AS A
//      LEFT JOIN m_actifity_report AS B ON A.id_outlet = B.id_outlet AND B.month = ".$monthd." AND B.year = ".$yeard."
//      LEFT JOIN d_day_actifity_report AS C ON B.id_actifity_report = C.id_actifity_report AND C.tanggal > 0 AND C.tanggal <= ".$dated."
//      {$list_outlet}
//      GROUP BY A.id_outlet
//      ORDER BY total_omset DESC
//      ";
//    $data2 = $this->global_models->get_query($query2);
    
//    $query3 = "
//      SELECT 
//      A.*,
//      C.rata_service, C.rata_belanja, C.service, C.service_belanja, C.belanja
//      FROM m_outlet AS A
//      LEFT JOIN m_actifity_report AS B ON A.id_outlet = B.id_outlet AND B.month = ".$monthd." AND B.year = ".$yeard."
//      LEFT JOIN d_day_actifity_report AS C ON B.id_actifity_report = C.id_actifity_report AND C.tanggal = 0
//      {$list_outlet}
//      GROUP BY A.id_outlet
//      ORDER BY total_omset DESC
//      ";
//    $data3 = $this->global_models->get_query($query3);
    $gt = $this->global_models->get_query("SELECT service, service_belanja"
      . " FROM d_day_actifity_report"
      . " ORDER BY service DESC LIMIT 0, 10");
//    $this->debug($data);
//    $this->debug($gt, true);
    $this->template->build("main", 
      array('message'     => $pesan,
            'url'         => base_url()."themes/srabon/",
            'menu_drop'   => $this->menu,
            'menu'        => "",
            'data'        => $data,
//            'data2'        => $data2,
            'hide'        => $hide,
            'year'        => $yeard,
            'month'        => $buland,
            'dated'        => $dated,
            'tdayd'        => $tdayd,
            'monthd'       => $monthd,
            'valtampil'        => $valtampil,
            'title_table' => "Monitor Aktifitas Outlet | Proyeksi Per Tanggal ".$comd,
            'foot'        => "",
            'costume_query' => $gt,
          ));
    $this->template
      ->set_layout('default')
      ->build("main");
	} 
  function laporan_tahunan($id_outlet, $tahun_select = 0){
    if($tahun_select == 0)
      $tahun_select = date("Y");
  $newdata = array(
      'outlet'  => $id_outlet
    );
    $this->session->set_userdata($newdata); 
    $outlet = $this->global_models->get("m_outlet", array("id_outlet" => $id_outlet));
//    foreach($outlet as $ot){
    $bulan = array(
        1 => "Januari",
        2 => "Februari",
        3 => "Maret",
        4 => "April",
        5 => "Mei",
        6 => "Juni",
        7 => "Juli",
        8 => "Agustus",
        9 => "September",
        10 => "Oktober",
        11 => "Nopember",
        12 => "Desember",
    );
    foreach($bulan as $kb => $bln){
      $dataunitentry[$kb] =
         $this->global_models->get_query(
            "SELECT 
              SUM(A.service) AS service,
              SUM(A.service_belanja) AS service_belanja,
              SUM(A.belanja) AS belanja,
              SUM(A.p_service) AS omset_service,
              SUM(A.p_service_plus) AS omset_service_plus,
              SUM(A.p_belanja_plus) AS omset_belanja_plus,
              SUM(A.p_belanja) AS omset_belanja,
              SUM(A.total_hpp) AS hpp,
              C.name,
              B.month,A.id_actifity_report, B.day_in_month, C.last_console, MAX(A.tanggal) max_tanggal
              FROM d_day_actifity_report AS A
              LEFT JOIN m_actifity_report AS B ON A.id_actifity_report = B.id_actifity_report
              LEFT JOIN m_outlet AS C ON B.id_outlet = C.id_outlet
              WHERE A.tanggal > 0
              AND C.id_outlet = {$id_outlet}
              AND B.month = {$kb}
              AND B.year = ".$tahun_select."
              GROUP BY A.id_actifity_report
              "
         );
       $datatarget[$kb] = 
         $this->global_models->get_query(
            "SELECT 
              (A.service + A.service_belanja) AS ue_proyeksi,
              A.service, A.service_belanja, A.belanja, A.rata_service, A.rata_belanja, A.hpp AS target_hpp
              FROM d_day_actifity_report AS A
              LEFT JOIN m_actifity_report AS B ON A.id_actifity_report = B.id_actifity_report
              LEFT JOIN m_outlet AS C ON B.id_outlet = C.id_outlet
              WHERE A.tanggal = 0
              AND C.id_outlet = {$id_outlet}
              AND B.month = {$kb}
              AND B.year = ".$tahun_select."
              GROUP BY A.id_actifity_report
              "
         );
    }
//    }
//    
    $tahun_change = $this->global_models->get_query("
      SELECT year FROM m_actifity_report GROUP BY year
      ORDER BY year DESC
      ");
    foreach($tahun_change AS $tc){
      $tahun_dropdown[$tc->year] = $tc->year;
    }
//    $this->debug($tahun_change, true);
//    $this->debug($dataunitentry, true);
    
    $this->template->build("laporan-tahunan", 
      array('message'     => $pesan,
            'url'         => base_url()."themes/srabon/",
            'menu_drop'   => $this->menu,
            'menu'        => "",
            'bulan'        => $bulan,
            'outlet'        => $outlet,
            'dataunitentry' =>  $dataunitentry,
            'datatarget' =>  $datatarget,
            'title_table' => "Laporan Tahunan ". $outlet[0]->name." ".date("Y"),
            'foot'        => "",
            'tahun'       => $tahun_dropdown,
            'tahun_select' => $tahun_select
          ));
    $this->template
      ->set_layout('default')
      ->build("laporan-tahunan");
  }
  function detail_bulan($bulan, $id_outlet){
    $detail = $this->global_models->get("m_actifity_report", array("id_outlet"  =>  $id_outlet, "month" => $bulan, "year" =>  date("Y")));
    $this->session->set_userdata(array("outlet" => $id_outlet));
    if($detail[0]->id_actifity_report)
      redirect("outlet/aktifitas/detail/".$detail[0]->id_actifity_report);
    else
      redirect("monitoring/laporan-tahunan/".$id_outlet);
  }
  
  function export_xls($tanggal = FALSE){
    if($tanggal === FALSE){
      $dated = date('d');
      $tdayd = date('t');
      $monthd = date('m');
      $yeard = date('Y');
      $buland = date('M');
      $comd = date('d M Y');
      $valtampil = date('Y-m-d');
    }
    else{
      $tanggal = str_replace("_", "-", $tanggal);
      $dated = date('d', strtotime($tanggal));
      $tdayd = date('t', strtotime($tanggal));
      $monthd = date('m', strtotime($tanggal));
      $yeard = date('Y', strtotime($tanggal));
      $buland = date('M', strtotime($tanggal));
      $comd = date('d M Y', strtotime($tanggal));
      $valtampil = date('Y-m-d', strtotime($tanggal));
    }
    $list_outlet = "";
    $hide = "block";
    if($this->global_models->get_field("m_privilege", "name", array("id_privilege" => $this->session->userdata('id_privilege'))) == "Outlet - Owner" OR $this->global_models->get_field("m_privilege", "name", array("id_privilege" => $this->session->userdata('id_privilege'))) == "Outlet - Kepala" OR $this->session->userdata('id') == 28){
      $list_outlet = "WHERE A.id_outlet IN (0";
      $no = 0;
      foreach($this->global_models->get_outlet_user($this->session->userdata('id')) as $value){
//        if($no > 0)
          $list_outlet .= ",";
        $list_outlet .= $value->id_outlet;
        $no++;
      }
      $list_outlet .= ")";
      $hide = "none";
//      $this->debug($list_outlet, true);
    }
	
	if($this->session->userdata('id') == 28){
		$hide = "block";
	}
    
    $query = "
      SELECT 
      A.*, 
      MAX(C.tanggal) AS tanggal_max,
      (SUM(C.service) + SUM(C.service_belanja) + SUM(C.belanja)) AS total_transaksi,
      (SUM(C.service) + SUM(C.service_belanja)) AS unit_entry,
      (SUM(C.p_service) + SUM(C.p_service_plus) + SUM(C.p_belanja_plus) + SUM(C.p_belanja)) AS total_omset,
      (SUM(C.service) + SUM(C.service_belanja)/".$dated." * ".$tdayd.") AS ue_omset,
      C.id_actifity_report
      FROM m_outlet AS A
      LEFT JOIN m_actifity_report AS B ON A.id_outlet = B.id_outlet AND B.month = ".$monthd." AND B.year = ".$yeard."
      LEFT JOIN d_day_actifity_report AS C ON B.id_actifity_report = C.id_actifity_report AND C.tanggal > 0 AND C.tanggal <= ".$dated."
      {$list_outlet}
      AND A.id_status_outlet = 0
      GROUP BY A.id_outlet
      ORDER BY ue_omset DESC
      
      ";
    $data = $this->global_models->get_query($query);
//    $this->debug($data, true);
    
    
    
    $objPHPExcel = $this->phpexcel;
      $objPHPExcel->getProperties()->setCreator("Mr Montir")
							 ->setLastModifiedBy("Mr Montir")
							 ->setTitle("Users Data")
							 ->setSubject("Users Data")
							 ->setDescription("Report users data.")
							 ->setKeywords("report users data")
							 ->setCategory("Users");

      $objPHPExcel->setActiveSheetIndex(0);
      
      $objPHPExcel->getActiveSheet()->mergeCells('A1:G2');
      $objPHPExcel->getActiveSheet()->setCellValue('A1', "Monitoring Outlet | Proyeksi per Tanggal {$comd}");
      $objPHPExcel->getActiveSheet()->getStyle('A1:G2')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
      $objPHPExcel->getActiveSheet()->getStyle('A1:G2')->getFill()->getStartColor()->setARGB('FF808080');
      $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
      $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
      $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
      
      $objPHPExcel->getActiveSheet()->setCellValue('A4', 'Tanggal Terakhir Input');
      $objPHPExcel->getActiveSheet()->setCellValue('B4', 'Outlet');
      $objPHPExcel->getActiveSheet()->setCellValue('C4', 'UT');
      $objPHPExcel->getActiveSheet()->setCellValue('D4', 'UE');
      $objPHPExcel->getActiveSheet()->setCellValue('E4', 'Omset');
      $objPHPExcel->getActiveSheet()->setCellValue('F4', 'UE Proyeksi');
      $objPHPExcel->getActiveSheet()->setCellValue('G4', 'Omset Proyeksi');
      $objPHPExcel->getActiveSheet()->getStyle('A4:G4')->applyFromArray(
          array(
            'font'    => array(
              'bold'      => true
            ),
            'alignment' => array(
              'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
            ),
            'borders' => array(
              'top'     => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
              ),
              'bottom'     => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
              ),
              'left'     => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
              ),
              'right'     => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
              ),
            ),
            'fill' => array(
              'type'       => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
                'rotation'   => 90,
              'startcolor' => array(
                'argb' => 'FFA0A0A0'
              ),
              'endcolor'   => array(
                'argb' => 'FFFFFFFF'
              )
            )
          )
      );
      $no = 5;
      foreach($data AS $dt){
        $objPHPExcel->getActiveSheet()->setCellValue('A'.$no, $dt->last_console);
        $objPHPExcel->getActiveSheet()->setCellValue('B'.$no, $dt->name);
        $objPHPExcel->getActiveSheet()->setCellValue('C'.$no, $dt->total_transaksi);
        $objPHPExcel->getActiveSheet()->setCellValue('D'.$no, $dt->unit_entry);
        $objPHPExcel->getActiveSheet()->setCellValue('E'.$no, $dt->total_omset);
        $objPHPExcel->getActiveSheet()->setCellValue('F'.$no, $dt->ue_omset);
        $objPHPExcel->getActiveSheet()->setCellValue('G'.$no, ($dt->total_omset/$dated * $tdayd));
        $no++;
      }
      
      $objPHPExcel->getActiveSheet()->getStyle('C5:G'.$no)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1);
      
      $objPHPExcel->getActiveSheet()->getStyle('A5:G'.$no)->applyFromArray(
          array(
            'borders' => array(
              'allborders'     => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
              ),
            ),
          )
      );
      
      $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
      $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
      $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
      $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
      $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
      $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
      $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
//      $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);
//      $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(30);
//      $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(50);
//      $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);
      
      $objPHPExcel->setActiveSheetIndex(0);
      header('Content-Type: application/vnd.ms-excel');
      header('Content-Disposition: attachment;filename="Monitoring'."-".date("Y m d").'"');
      header('Cache-Control: max-age=0');
      $objWriter = PHPExcel_IOFactory::createWriter($this->phpexcel, 'Excel5');
//$objWriter->save(str_replace('.php', '.xls', __FILE__));
      $objWriter->save('php://output');die;
  }
  
  function export_xls_tahunan($id_outlet, $tahun_select = 0){
    if($tahun_select == 0)
      $tahun_select = date("Y");
  $newdata = array(
      'outlet'  => $id_outlet
    );
    $this->session->set_userdata($newdata); 
    $outlet = $this->global_models->get("m_outlet", array("id_outlet" => $id_outlet));
//    foreach($outlet as $ot){
    $bulan = array(
        1 => "Januari",
        2 => "Februari",
        3 => "Maret",
        4 => "April",
        5 => "Mei",
        6 => "Juni",
        7 => "Juli",
        8 => "Agustus",
        9 => "September",
        10 => "Oktober",
        11 => "Nopember",
        12 => "Desember",
    );
    foreach($bulan as $kb => $bln){
      $dataunitentry[$kb] =
         $this->global_models->get_query(
            "SELECT 
              SUM(A.service) AS service,
              SUM(A.service_belanja) AS service_belanja,
              SUM(A.belanja) AS belanja,
              SUM(A.p_service) AS omset_service,
              SUM(A.p_service_plus) AS omset_service_plus,
              SUM(A.p_belanja_plus) AS omset_belanja_plus,
              SUM(A.p_belanja) AS omset_belanja,
              SUM(A.total_hpp) AS hpp,
              C.name,
              B.month,A.id_actifity_report, B.day_in_month, C.last_console, MAX(A.tanggal) max_tanggal
              FROM d_day_actifity_report AS A
              LEFT JOIN m_actifity_report AS B ON A.id_actifity_report = B.id_actifity_report
              LEFT JOIN m_outlet AS C ON B.id_outlet = C.id_outlet
              WHERE A.tanggal > 0
              AND C.id_outlet = {$id_outlet}
              AND B.month = {$kb}
              AND B.year = ".$tahun_select."
              GROUP BY A.id_actifity_report
              "
         );
       $datatarget[$kb] = 
         $this->global_models->get_query(
            "SELECT 
              (A.service + A.service_belanja) AS ue_proyeksi,
              A.service, A.service_belanja, A.belanja, A.rata_service, A.rata_belanja, A.hpp AS target_hpp
              FROM d_day_actifity_report AS A
              LEFT JOIN m_actifity_report AS B ON A.id_actifity_report = B.id_actifity_report
              LEFT JOIN m_outlet AS C ON B.id_outlet = C.id_outlet
              WHERE A.tanggal = 0
              AND C.id_outlet = {$id_outlet}
              AND B.month = {$kb}
              AND B.year = ".$tahun_select."
              GROUP BY A.id_actifity_report
              "
         );
    }
//    $this->debug($outlet);
//    $this->debug($outlet);
//    $this->debug($dataunitentry);
//    $this->debug($datatarget, true);
    
    $objPHPExcel = $this->phpexcel;
      $objPHPExcel->getProperties()->setCreator("Mr Montir")
							 ->setLastModifiedBy("Mr Montir")
							 ->setTitle("Users Data")
							 ->setSubject("Users Data")
							 ->setDescription("Report users data.")
							 ->setKeywords("report users data")
							 ->setCategory("Users");

      $objPHPExcel->setActiveSheetIndex(0);
      
      $objPHPExcel->getActiveSheet()->mergeCells('A1:M2');
      $objPHPExcel->getActiveSheet()->setCellValue('A1', "LAPORAN TAHUNAN {$outlet[0]->name} {$tahun_select}");
      $objPHPExcel->getActiveSheet()->getStyle('A1:M2')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
      $objPHPExcel->getActiveSheet()->getStyle('A1:M2')->getFill()->getStartColor()->setARGB('FF808080');
      $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
      $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
      $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
      
      $objPHPExcel->getActiveSheet()->setCellValue('A4', 'Bulan');
      $objPHPExcel->getActiveSheet()->setCellValue('B4', 'Target UE');
      $objPHPExcel->getActiveSheet()->setCellValue('C4', 'UE');
      $objPHPExcel->getActiveSheet()->setCellValue('D4', 'UE Proyeksi');
      $objPHPExcel->getActiveSheet()->setCellValue('E4', 'Target UT');
      $objPHPExcel->getActiveSheet()->setCellValue('F4', 'UT');
      $objPHPExcel->getActiveSheet()->setCellValue('G4', 'UT Proyeksi');
      $objPHPExcel->getActiveSheet()->setCellValue('H4', 'Target Omset');
      $objPHPExcel->getActiveSheet()->setCellValue('I4', 'Omset');
      $objPHPExcel->getActiveSheet()->setCellValue('J4', 'Omset Proyeksi');
      $objPHPExcel->getActiveSheet()->setCellValue('K4', 'Target Gross Margin');
      $objPHPExcel->getActiveSheet()->setCellValue('L4', 'Gross Margin');
      $objPHPExcel->getActiveSheet()->setCellValue('M4', 'Gross Margin Proyeksi');
      $objPHPExcel->getActiveSheet()->getStyle('A4:M4')->applyFromArray(
          array(
            'font'    => array(
              'bold'      => true
            ),
            'alignment' => array(
              'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
            ),
            'borders' => array(
              'top'     => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
              ),
              'bottom'     => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
              ),
              'left'     => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
              ),
              'right'     => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
              ),
            ),
            'fill' => array(
              'type'       => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
                'rotation'   => 90,
              'startcolor' => array(
                'argb' => 'FFA0A0A0'
              ),
              'endcolor'   => array(
                'argb' => 'FFFFFFFF'
              )
            )
          )
      );
      $no = 5;
      foreach($bulan AS $key => $bln){
        
        $ue = $dataunitentry[$key][0]->service + $dataunitentry[$key][0]->service_belanja;
        $target_ue = $datatarget[$key][0]->service + $datatarget[$key][0]->service_belanja;
        $proyeksi_ue = $ue/$dataunitentry[$key][0]->max_tanggal * $dataunitentry[$key][0]->day_in_month;
        
        $ut = $dataunitentry[$key][0]->belanja + $dataunitentry[$key][0]->service_belanja;
        $target_ut = $datatarget[$key][0]->belanja + $datatarget[$key][0]->service_belanja;
        $proyeksi_ut = $ut/$dataunitentry[$key][0]->max_tanggal * $dataunitentry[$key][0]->day_in_month;
        
        $omset = $dataunitentry[$key][0]->omset_service + $dataunitentry[$key][0]->omset_service_plus + $dataunitentry[$key][0]->omset_belanja_plus + $dataunitentry[$key][0]->omset_belanja;
                  
        $target_omset_service = $datatarget[$key][0]->service * $datatarget[$key][0]->rata_service;
        $target_omset_service_plus = $datatarget[$key][0]->service_belanja * $datatarget[$key][0]->rata_service;
        $target_omset_belanja_plus = $datatarget[$key][0]->service_belanja * $datatarget[$key][0]->rata_belanja;
        $target_omset_belanja = $datatarget[$key][0]->belanja * $datatarget[$key][0]->rata_belanja;

        $target_omset = $target_omset_service + $target_omset_service_plus + $target_omset_belanja_plus + $target_omset_belanja;
        $proyeksi_omset = $omset/$dataunitentry[$key][0]->max_tanggal * $dataunitentry[$key][0]->day_in_month;
        
        $gm = $omset - $dataunitentry[$key][0]->hpp;
        $target_gm = $target_omset - ($target_omset - ($datatarget[$key][0]->target_hpp/100 * $target_omset));
        $proyeksi_gm = $gm/$dataunitentry[$key][0]->max_tanggal * $dataunitentry[$key][0]->day_in_month;
        
        $objPHPExcel->getActiveSheet()->setCellValue('A'.$no, $bln);
        $objPHPExcel->getActiveSheet()->setCellValue('B'.$no, $target_ue);
        $objPHPExcel->getActiveSheet()->setCellValue('C'.$no, $ue);
        $objPHPExcel->getActiveSheet()->setCellValue('D'.$no, $proyeksi_ue);
        $objPHPExcel->getActiveSheet()->setCellValue('E'.$no, $target_ut);
        $objPHPExcel->getActiveSheet()->setCellValue('F'.$no, $ut);
        $objPHPExcel->getActiveSheet()->setCellValue('G'.$no, $proyeksi_ut);
        $objPHPExcel->getActiveSheet()->setCellValue('H'.$no, $target_omset);
        $objPHPExcel->getActiveSheet()->setCellValue('I'.$no, $omset);
        $objPHPExcel->getActiveSheet()->setCellValue('J'.$no, $proyeksi_omset);
        $objPHPExcel->getActiveSheet()->setCellValue('K'.$no, $target_gm);
        $objPHPExcel->getActiveSheet()->setCellValue('L'.$no, $gm);
        $objPHPExcel->getActiveSheet()->setCellValue('M'.$no, $proyeksi_gm);
        $no++;
      }
      
      $objPHPExcel->getActiveSheet()->getStyle('C5:M'.$no)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1);
      
      $objPHPExcel->getActiveSheet()->getStyle('A5:M'.$no)->applyFromArray(
          array(
            'borders' => array(
              'allborders'     => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
              ),
            ),
          )
      );
      
      $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
      $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
      $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
      $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
      $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
      $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
      $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
      $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
      $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
      $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true);
      $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setAutoSize(true);
      $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setAutoSize(true);
      $objPHPExcel->getActiveSheet()->getColumnDimension('M')->setAutoSize(true);
//      $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);
//      $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(30);
//      $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(50);
//      $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);
      
      $objPHPExcel->setActiveSheetIndex(0);
      header('Content-Type: application/vnd.ms-excel');
      header('Content-Disposition: attachment;filename="Monitoring'."-".date("Y m d").'"');
      header('Cache-Control: max-age=0');
      $objWriter = PHPExcel_IOFactory::createWriter($this->phpexcel, 'Excel5');
//$objWriter->save(str_replace('.php', '.xls', __FILE__));
      $objWriter->save('php://output');die;
  }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */